<?php
/**
 * 《材料信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Material.class.php 1020 2010-03-19 07:04:54Z sunxy $
 */
require_once(dirname(__FILE__) . '/MaterialUtil.class.php');

class Pic extends MaterialUtil {
	public $tbl = Constant::tbl_material_pic;

	var $info_dir = 'material';

	public $orderby = array(
		'-' => '请选择',
		'material' => '材料',
		'orderby' => '排序',
		'1' => '创建时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'material/pic/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$material_s = $request['material_s'];

		$where = " WHERE 1=1";

		if (!empty($material_s)) {
			$where .= " AND material = '" . $material_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['m'] = $this->id(Constant::tbl_material, $row['material']);

				$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['material'], $row['name']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'material/pic/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'material/pic/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];

		$name = $request['name'];

		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$standard = $request['standard'];
		$unit = $request['unit'];
		$price = $request['price'];
		$content = $request['content'];

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET code = '%s', name = '%s', category = '%s', type = '%s', standard = '%s', unit = '%s', price = '%s', content = '%s'";
		$fmt .= ", status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $name, $category, $type, $standard, $unit, $price, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return true;
		}
			
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		// clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		require_once 'Material.class.php';
		$material = new Material();
		$controller->assign_by_ref('material', $material->_view($row['material']));

		$controller->display($request, 'material/pic/view');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		require_once 'Material.class.php';
		$material = new Material();
		$row = $material->_view($id);

		$controller->assign_by_ref('material', $row);

		$this->_my($controller, $row);

		$controller->display($request, 'material/pic/add');
		return true;
	}

	function _my($controller, $data) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE material = '%s' ORDER BY orderby DESC";
		$sql = sprintf($fmt, Constant::tbl_material_pic, $data['id']);
		$result = $db->query($sql);

		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['material'], $row['name']);
			$row['size_t'] = printsize($row['size']);

			$ret[] = $row;
		}

		$controller->assign_by_ref('pic', $ret);
	}

	function ajax_upload($controller, $request) {
		$s = print_r($request, true);
		debug($s);

		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			die($file->getMessage());
		}

		if ($file->isValid()) {
			$now = date('Y-m-d H:i:s');

			$id = $request['id'];
			$name = $request['name'];
			if (empty($name) || $name == '-') {
				$name = 'uniq';
			}

			$dir = Download::dir($this->info_dir, $now, $id);
			debug($dir);
			if (!@file_exists($dir)) {
				umask();
				mkdir($dir, 0777, true);
			}

			$file->setName($name);
			$filename = $file->moveTo($dir);
			if (PEAR::isError($filename)){
				die($filename->getMessage());
			}

			$filesize = $file->upload['size'];

			$db = newdb($this->dsn);
			$fmt = "INSERT INTO %s(material, name, size, createtime)";
			$fmt .= " VALUES('%s', '%s', '%s', now())";
			$sql = sprintf($fmt, Constant::tbl_material_pic, $id, $filename, $filesize);
			debug($sql);
			$db->query($sql);
		}

		echo "1";
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function a($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$id = $request['id'];
		$material = $request['material'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s set orderby = 5 WHERE material = '%s'";
		$sql = sprintf($fmt, $this->tbl, $material);
		$db->query($sql);

		$fmt = "UPDATE %s set orderby = 1 WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}
?>